﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace He.Web.UI
{
    /// <summary>
    /// 输入辅助类(用以统一输入验证等)
    /// </summary>
    public class InputUtil
    {
        #region GET请求
        /// <summary>
        /// 根据键名获取请求(Request)的相应参数值(数字型)
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public static int Get(string key, int defaultValue)
        {
            string text = HttpContext.Current.Request.QueryString[key];

            int result;
            bool flag = int.TryParse(text, out result);
            return flag ? result : defaultValue;
        }

        /// <summary>
        /// 根据键名获取请求(Request)的相应参数值(字符串型)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Get(string key)
        {
            string text = HttpContext.Current.Request.QueryString[key];
            if (string.IsNullOrEmpty(text))
                return string.Empty;

            return text.Trim().Replace("'", "");
        }
        /// <summary>
        /// 根据键名获取请求(Request)的相应参数值(字符串型)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string Get(string key, string defaultValue)
        {
            string text = HttpContext.Current.Request.QueryString[key];
            if (string.IsNullOrEmpty(text))
                return defaultValue;

            return text.Trim().Replace("'", "");
        }
        /// <summary>
        /// 根据键名获取请求(Request)的相应参数值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="filterXss">是否进行Xss过滤</param>
        /// <returns></returns>
        //public string Get(string key, bool filterXss)
        //{
        //    if (filterXss)
        //    {
        //        return AntiXss.HtmlEncode(this.Get(key));
        //    }
        //    else
        //    {
        //        return this.Get(key);
        //    }
        //}

        public static int QueryString(string key, int defaultValue)
        {
            return Get(key, defaultValue);
        }

        public static string QueryString(string key)
        {
            return Get(key);
        }

        //public string QueryString(string key, bool filterXss)
        //{
        //    return this.Get(key, filterXss);
        //}

        #endregion

        #region POST请求
        public static string Post(string key)
        {
            string text = HttpContext.Current.Request.Form[key];
            if (string.IsNullOrEmpty(text))
                return string.Empty;

            return text.Trim().Replace("'", "");
        }

        public static string Form(string key, string defaultValue)
        {
            string text = HttpContext.Current.Request.Form[key];
            if (string.IsNullOrEmpty(text))
                return defaultValue;

            return text.Trim().Replace("'", "");
        }

        public static int Form(string key, int defaultValue)
        {
            string text = HttpContext.Current.Request.Form[key];
            int result;
            if (int.TryParse(text, out result))
            {
                return result;
            }
            return defaultValue;
        }

        public static string Form(string key)
        {
            return Post(key);
        }

        //public string Form(string key, bool filterXss)
        //{
        //    return this.Post(key, filterXss);
        //}
        #endregion

    }
}
